EntityManager<TContext, TEntity>

Provides a base class for entity-specific business logic managers with built-in CRUD operations, audit trail support, and lifecycle event hooks. Handles common entity operations and automatically manages audit fields for entities that implement auditing interfaces.

Syntax

public abstract class EntityManager<TContext, TEntity> : ManagerBase<TContext> where TContext : DbContext where TEntity : class

Inheritance

Constructors

EntityManager

public EntityManager(TContext dataContext, IMessagePublisher messagePublisher)
Initializes a new instance of the class.

Parameters

  • dataContext TContext: The database context instance for data operations. Should be injected by the DI container.
  • messagePublisher IMessagePublisher: The message publisher instance for publishing events. Should be injected by the DI container.

Methods

DeleteAsync

public Task<bool> DeleteAsync(List<TEntity> entities, bool save = true)
Delete all from a list with optional save operation.

Parameters

Returns

Task<bool>

DeleteAsync

public Task<bool> DeleteAsync(List<TEntity> entities, TContext context, bool save = true)
Delete all from a list with optional save operation using a specified .

Parameters

Returns

Task<bool>

DeleteAsync

public Task<bool> DeleteAsync(TEntity entity, bool save = true)
Delete a specific with optional save operation.

Parameters

  • entity TEntity:
  • save bool:

Returns

Task<bool>

DeleteAsync

public Task<bool> DeleteAsync(TEntity entity, TContext context, bool save = true)
Delete a specific with optional save operation using a specified .

Parameters

  • entity TEntity:
  • context TContext:
  • save bool:

Returns

Task<bool>

DirectDelete

public int DirectDelete(Expression<Func<TEntity, bool>> predicate)
Delete entities returned by the specified query without individual entity processing.

Parameters

Returns

int

DirectDeleteAsync

public Task<int> DirectDeleteAsync(Expression<Func<TEntity, bool>> predicate)
Delete entities returned by the specified query without individual entity processing.

Parameters

Returns

Task<int>

DirectUpdate

public int DirectUpdate(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression)
Executes a direct UPDATE query on the database without returning objects or processing them through the interceptors.

Parameters

Returns

int

DirectUpdateAsync

public Task<int> DirectUpdateAsync(Expression<Func<TEntity, bool>> predicate, Expression<Func<TEntity, TEntity>> updateExpression)
Executes a direct UPDATE query on the database without returning objects or processing them through the interceptors.

Parameters

Returns

Task<int>

InsertAsync

public Task<bool> InsertAsync(List<TEntity> entities, bool save = true)
Inserts a collection of entities into the database with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks for each entity.

Parameters

  • entities List<TEntity>: The collection of entities to be inserted.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entities were successfully inserted; otherwise, false.

InsertAsync

public Task<bool> InsertAsync(List<TEntity> entities, TContext context, bool save = true)
Inserts a collection of entities into the database using a specified context with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks for each entity.

Parameters

  • entities List<TEntity>: The collection of entities to be inserted.
  • context TContext: The database context to use for the operation.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entities were successfully inserted; otherwise, false.

InsertAsync

public Task<bool> InsertAsync(TEntity entity, bool save = true)
Inserts a single entity into the database with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks.

Parameters

  • entity TEntity: The entity to be inserted.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entity was successfully inserted; otherwise, false.

InsertAsync

public Task<bool> InsertAsync(TEntity entity, TContext context, bool save = true)
Inserts a single entity into the database using a specified context with optional save operation. Executes the OnInsertingAsync and OnInsertedAsync lifecycle hooks.

Parameters

  • entity TEntity: The entity to be inserted.
  • context TContext: The database context to use for the operation.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entity was successfully inserted; otherwise, false.

OnDeletedAsync

public virtual Task OnDeletedAsync(List<TEntity> entities)
Called after successfully deleting a collection of entities from the database. Applies OnDeletedAsync logic to each entity in the collection.

Parameters

  • entities List<TEntity>: The collection of entities that were deleted.

Returns

Task

OnDeletedAsync

public virtual Task<bool> OnDeletedAsync(TEntity entity)
Called after successfully deleting an entity from the database. Use this method for post-deletion business logic such as cleanup operations, sending notifications, or triggering external systems.

Parameters

  • entity TEntity: The entity that was deleted.

Returns

Task<bool> True if post-deletion processing was successful; otherwise, false.

OnDeletingAsync

public virtual Task OnDeletingAsync(List<TEntity> entities)
Called before deleting a collection of entities from the database. Applies OnDeletingAsync logic to each entity in the collection.

Parameters

  • entities List<TEntity>: The collection of entities to be deleted.

Returns

Task

OnDeletingAsync

public virtual Task OnDeletingAsync(TEntity entity)
Called before deleting an entity from the database. Override this method to add custom business logic or validation before deletion.

Parameters

  • entity TEntity: The entity to be deleted.

Returns

Task

OnInsertedAsync

public virtual Task OnInsertedAsync(List<TEntity> entities)
Called after successfully inserting a collection of entities into the database. Applies OnInsertedAsync logic to each entity in the collection.

Parameters

  • entities List<TEntity>: The collection of entities that were inserted.

Returns

Task

OnInsertedAsync

public virtual Task<bool> OnInsertedAsync(TEntity entity)
Called after successfully inserting an entity into the database. Use this method for post-insertion business logic such as sending notifications, publishing events, or triggering external systems.

Parameters

  • entity TEntity: The entity that was inserted.

Returns

Task<bool> True if post-insertion processing was successful; otherwise, false.

OnInsertingAsync

public Task OnInsertingAsync(List<TEntity> entities)
Called before inserting a collection of entities into the database. Applies OnInsertingAsync logic to each entity in the collection.

Parameters

  • entities List<TEntity>: The collection of entities to be inserted.

Returns

Task

OnInsertingAsync

public virtual Task OnInsertingAsync(TEntity entity)
Called before inserting an entity into the database. Automatically handles audit field population and user tracking for entities implementing the appropriate interfaces.

Parameters

  • entity TEntity: The entity to be inserted.

Returns

Task

OnUpdatedAsync

public virtual Task OnUpdatedAsync(List<TEntity> entities)
Called after successfully updating a collection of entities in the database. Applies OnUpdatedAsync logic to each entity in the collection.

Parameters

  • entities List<TEntity>: The collection of entities that were updated.

Returns

Task

OnUpdatedAsync

public virtual Task<bool> OnUpdatedAsync(TEntity entity)
Called after successfully updating an entity in the database. Use this method for post-update business logic such as sending notifications, publishing events, or triggering external systems.

Parameters

  • entity TEntity: The entity that was updated.

Returns

Task<bool> True if post-update processing was successful; otherwise, false.

OnUpdatingAsync

public virtual Task OnUpdatingAsync(List<TEntity> entities)
Called before updating a collection of entities in the database. Applies OnUpdatingAsync logic to each entity in the collection.

Parameters

  • entities List<TEntity>: The collection of entities to be updated.

Returns

Task

OnUpdatingAsync

public virtual Task OnUpdatingAsync(TEntity entity)
Called before updating an entity in the database. Automatically handles audit field population and user tracking for entities implementing the appropriate interfaces.

Parameters

  • entity TEntity: The entity to be updated.

Returns

Task

ResetAuditProperties<TDbObservable>

public void ResetAuditProperties<TDbObservable>(TDbObservable entity) where TDbObservable : DbObservableObject
Resets audit properties to an “Inserted” state by setting creation fields and clearing update fields. Sets CreatedById and DateCreated to current values, while clearing UpdatedById and DateUpdated.

Parameters

  • entity TDbObservable: The entity whose audit properties should be reset.

UpdateAsync

public Task<bool> UpdateAsync(List<TEntity> entities, bool save = true)
Updates a collection of entities in the database with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks for each entity.

Parameters

  • entities List<TEntity>: The collection of entities to be updated.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entities were successfully updated; otherwise, false.

UpdateAsync

public Task<bool> UpdateAsync(List<TEntity> entities, TContext context, bool save = true)
Updates a collection of entities in the database using a specified context with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks for each entity.

Parameters

  • entities List<TEntity>: The collection of entities to be updated.
  • context TContext: The database context to use for the operation.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entities were successfully updated; otherwise, false.

UpdateAsync

public Task<bool> UpdateAsync(TEntity entity, bool save = true)
Updates a single entity in the database with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks.

Parameters

  • entity TEntity: The entity to be updated.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entity was successfully updated; otherwise, false.

UpdateAsync

public Task<bool> UpdateAsync(TEntity entity, TContext context, bool save = true)
Updates a single entity in the database using a specified context with optional save operation. Executes the OnUpdatingAsync and OnUpdatedAsync lifecycle hooks.

Parameters

  • entity TEntity: The entity to be updated.
  • context TContext: The database context to use for the operation.
  • save bool: Whether to immediately save changes to the database. Defaults to true.

Returns

Task<bool> True if the entity was successfully updated; otherwise, false.

Remarks

This manager provides comprehensive entity lifecycle management including:
  • Automatic audit trail creation for entities implementing ,
  • User tracking for entities implementing ,
  • Virtual hooks for custom business logic before and after CRUD operations
  • Batch operations support for improved performance
  • Thread-safe interface caching for performance optimization